package com.ybw.utils;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class XMLParser {
    public static void main(String[] args) throws DocumentException {
        SAXReader sax=new SAXReader();//创建一个SAXReader对象
        File xmlFile=new File("c:\\workspace\\test.xml");//根据指定的路径创建file对象
        Document document=sax.read(xmlFile);//获取document对象,如果文档无节点，则会抛出Exception提前结束
        Element root=document.getRootElement();//获取根节点
        // 1.输出映射字符串
        List<Element> childNodes = root.elements();
        List<String> titles = new ArrayList<>();
        for(Element node : childNodes){
            List<Attribute> attributes = node.attributes();
            String name = null;
            String title = null;
            StringBuilder sb = new StringBuilder();
            boolean dateFlag = false;
            for (Attribute attr : attributes) {
                if ("name".equals(attr.getName())) {
                    name = attr.getValue();
                }
                if ("title".equals(attr.getName())) {
                    title = attr.getValue();
                    titles.add(title);
                }
                if ("use".equals(attr.getName()) && attr.getValue().contains("date")) {
                    dateFlag = true;
                }
                if ("type".equals(attr.getName()) && attr.getValue().contains("date")) {
                    dateFlag = true;
                }            }
            if (name == null || title == null) {
                System.out.println();
            }
            sb.append(name);
            sb.append("-");
            sb.append(title);
            if (dateFlag) {
                sb.append("-");
                sb.append("date");
            }
            sb.append(";");
            System.out.print(sb);
        }
        System.out.println();
        // 2.生成excel表头
        StringBuilder sb = new StringBuilder();
        for(String title : titles) {
            sb.append(title);
            sb.append(";");
        }
        String headerStr = sb.substring(0, sb.length()-1);
        System.out.println(headerStr);
        System.out.println();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    }
}
